//1.引入 express
const express = require('express')



//2.创建应用对象
const app = express();
//3.创建路由规则
//request  是对请求报文的封装
//response 是对响应报文的封装
app.get('/',(request,response)=>[
    //设置响应
    response.send('hello express')
])

app.get('/server',(requets,response)=>{
    //设置响应头
    response.setHeader('Access-Control-Allow-Origin','*')
    
    //设置响应体
    response.send('dadaadda ajax')

})




app.all('/server',(request,response)=>{

    //跨域
    response.setHeader('Access-Control-Allow-Origin','*')
    //自定义请求头
    response.setHeader('Access-Control-Allow-Headers','*')
    //响应数据
    response.send("鬼鬼鬼~")


})



app.post('/server-json',(request,response)=>{

    //跨域
    response.setHeader('Access-Control-Allow-Origin','*')
    //响应数据
    const person ={
        name:"jack",
        age:18,
        addresss:"上海-黄埔"
    }
    response.send(JSON.stringify(person))
})


app.get('/delay',(request,response)=>{
    //跨域
    response.setHeader('Access-Control-Allow-Origin','*')
    setTimeout(()=>{
        response.send("延时请求~")
    },2500)
   
})



app.all('/server-json',(request,response)=>{

    //跨域
    response.setHeader('Access-Control-Allow-Origin','*')
    response.setHeader('Access-Control-Allow-Headers','*')
    //响应数据
    const person ={
        name:"jack",
        age:18,
        addresss:"上海-黄埔"
    }
    setTimeout(()=>{
        response.send(JSON.stringify(person))
    },1000)
    
})





app.get('/axios-server',(request,response)=>{

    //跨域
    response.setHeader('Access-Control-Allow-Origin','*')
    //响应数据
    const person ={
        name:"jack",
        age:18,
        addresss:"上海-黄埔"
    }
    response.send(JSON.stringify(person))
})


app.all('/axios-post',(request,response)=>{

    //跨域
    response.setHeader('Access-Control-Allow-Origin','*')
    response.setHeader('Access-Control-Allow-Headers','*')


    //响应数据
    const person ={
        name:"mary",
        age:18,
        addresss:"北京-北大"
    }
    response.send(JSON.stringify(person))
})

//4.监听端口，调用回调
app.listen('8000',()=>{
    console.log('服务已经启动,8000,服务监听中')
})


